package io.renren.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import io.renren.annotation.Login;
import io.renren.common.constant.Constant;
import io.renren.common.utils.Result;
import io.renren.modules.health.dto.HmQuestionnaireDTO;
import io.renren.modules.health.dto.HmQuestionnaireResponse;
import io.renren.modules.health.entity.HmQuestionnaireEntity;
import io.renren.modules.health.service.FoodPlanService;
import io.renren.modules.health.service.HmQuestionnaireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 调查问卷测评结果
 */
@RestController
@RequestMapping("api/hmquestionnaire")
@Api(tags = "调查问卷测评结果")
public class HmQuestionnaireController {
    private static final Logger logger = LoggerFactory.getLogger(HmQuestionnaireController.class);

    static final Map<String, Map<String,Object>> questionsMap = new HashMap<String, Map<String,Object>>();
    @Autowired
    private HmQuestionnaireService hmQuestionnaireService;
    @Autowired
    private FoodPlanService foodPlanService;

    @Login
    @GetMapping("{cusId}")
    @ApiOperation("获取最新问卷结果")
    public Result<HashMap<String, HmQuestionnaireResponse>> getNewestByCusId(@PathVariable("cusId") Long cusId) {
        List<HmQuestionnaireEntity> hmQuestionnaireEntityList = hmQuestionnaireService.getNewestByCusId(cusId);
        if (hmQuestionnaireEntityList.isEmpty()) {
            return new Result<>();
        }
        return new Result<HashMap<String, HmQuestionnaireResponse>>().ok(JSON.parseObject(hmQuestionnaireEntityList.get(0).getResult(),
                new TypeReference<HashMap<String, HmQuestionnaireResponse>>() {}));
    }

    @Login
    @PostMapping
    @ApiOperation("保存")
    public Result<HashMap<String, HmQuestionnaireResponse>> save(@RequestBody HmQuestionnaireDTO dto) {
        logger.info("save.dto:{}",JSON.toJSONString(dto));
        HashMap<String, HmQuestionnaireResponse> responseHashMap = new HashMap<>();
        scopeComputed(dto,responseHashMap);
        HmQuestionnaireEntity hmQuestionnaireEntity = new HmQuestionnaireEntity();
        hmQuestionnaireEntity.setResult(JSON.toJSONString(responseHashMap));
        hmQuestionnaireEntity.setCusId(dto.getCusId());
        hmQuestionnaireEntity.setIllIds(dto.getQuestionList().get(27).getAnswer());
        hmQuestionnaireService.insert(hmQuestionnaireEntity);
        logger.info("save.responseHashMap:{}",JSON.toJSONString(responseHashMap));
        foodPlanService.genCustomerFoodPlan(dto.getCusId(), true);
        return new Result<HashMap<String, HmQuestionnaireResponse>>().ok(responseHashMap);
    }

    /**
     * 分数计算
     * @param dto
     * @param responseHashMap
     */
    private void scopeComputed(HmQuestionnaireDTO dto, HashMap<String, HmQuestionnaireResponse> responseHashMap) {
        HmQuestionnaireResponse food = new HmQuestionnaireResponse();
        HmQuestionnaireResponse smoke = new HmQuestionnaireResponse();
        HmQuestionnaireResponse drink = new HmQuestionnaireResponse();
        HmQuestionnaireResponse sport = new HmQuestionnaireResponse();
        HmQuestionnaireResponse pressure = new HmQuestionnaireResponse();
        HmQuestionnaireResponse all = new HmQuestionnaireResponse();
        responseHashMap.put(Constant.HealthEnum.FOOD.toString(),food);
        responseHashMap.put(Constant.HealthEnum.SMOKE.toString(),smoke);
        responseHashMap.put(Constant.HealthEnum.DRINK.toString(),drink);
        responseHashMap.put(Constant.HealthEnum.SPORT.toString(),sport);
        responseHashMap.put(Constant.HealthEnum.PRESSURE.toString(),pressure);
        responseHashMap.put(Constant.HealthEnum.ALL.toString(),all);

        //各项分数统计
        dto.getQuestionList().forEach(question -> {
            logger.info("question:{}",question);
            if (Integer.parseInt(question.getSerialNo()) > 27) {
                all.setScope(all.getScope() + question.getAnswer().split(",").length);
                return;
            }
            Map<String, Object> qMap = questionsMap.get(question.getSerialNo());
            if (qMap.get("type").equals(Constant.HealthEnum.FOOD)) {
                food.setScope(food.getScope() + ((Integer) qMap.get("opt" + question.getAnswer())));
            } else if (qMap.get("type").equals(Constant.HealthEnum.SMOKE)) {
                smoke.setScope(smoke.getScope() + ((Integer) qMap.get("opt" + question.getAnswer())));
            } else if (qMap.get("type").equals(Constant.HealthEnum.DRINK)) {
                drink.setScope(drink.getScope() + ((Integer) qMap.get("opt" + question.getAnswer())));
            } else if (qMap.get("type").equals(Constant.HealthEnum.SPORT)) {
                sport.setScope(sport.getScope() + ((Integer) qMap.get("opt" + question.getAnswer())));
            }  else if (qMap.get("type").equals(Constant.HealthEnum.PRESSURE)) {
                pressure.setScope(pressure.getScope() + ((Integer) qMap.get("opt" + question.getAnswer())));
            } else {
                all.setScope(all.getScope() + ((Integer) qMap.get("opt" + question.getAnswer())));
            }
        });
        all.setScope(all.getScope() + food.getScope() + smoke.getScope() + drink.getScope() + sport.getScope() + pressure.getScope());
        if (drink.getScope() < 1) {
            drink.setResult("无风险");
            drink.setDesc("无风险");
        } else {
            drink.setResult("有风险");
            drink.setDesc("饮酒对您的健康有一定风险。");
        }
        if (smoke.getScope() < 1) {
            smoke.setResult("无风险");
            smoke.setDesc("无风险");
        } else {
            smoke.setResult("有风险");
            smoke.setDesc("吸烟对您的健康存在一定风险。");
        }
        if (pressure.getScope() < 4) {
            pressure.setResult("压力小");
            pressure.setDesc("压力感受处于心理舒适区，保持良好的压力状态有利于身心健康。");
        } else if (pressure.getScope() < 8){
            pressure.setResult("压力在正常范围内");
            pressure.setDesc("压力感受属于适度和安全范围内，但某些地方需要改进。");
        } else {
            pressure.setResult("压力大");
            pressure.setDesc("压力较大，长期以往会对健康产生影响，需要寻找策略以减轻压力。");
        }

        if (sport.getScope() <= 2) {
            sport.setResult("低风险");
            sport.setDesc("体力活动充足，对健康有益。");
        } else if (sport.getScope() <= 4){
            sport.setResult("中风险");
            sport.setDesc("有一定里的体力活动，发生代谢性疾病的风险较低");
        } else {
            sport.setResult("高风险");
            sport.setDesc("体力活动严重缺乏，发生代谢性疾病的风险很高");
        }

        if (food.getScope() < 3) {
            food.setResult("饮食习惯较好");
            food.setDesc("饮食习惯较好，对身体健康有益");
        } else if (food.getScope() < 8){
            food.setResult("饮食习惯欠佳");
            food.setDesc("饮食习惯欠佳，可能会增加疾病发生的风险");
        } else {
            food.setResult("饮食习惯不好");
            food.setDesc("饮食习惯不好，会增加疾病发生的风险");
        }

        int scope = 100 - all.getScope();
        if (scope <= 53) {
            all.setResult("高风险");
            all.setDesc("您的分值目前在高风险区间，整体健康状态不佳，需要引起关注。");
        }
        if (scope > 53){
            all.setResult("中风险");
            all.setDesc("您的分值目前在中度风险区间，请关注健康问题，保持健康的生活方式!");
        }
        if (scope >= 65) {
            all.setResult("低风险");
            all.setDesc("您的分值目前在低风险区间，请保持健康的生活方式!");
        }

    }

    static {
        Map<String, Object> question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("type",Constant.HealthEnum.SMOKE);
        questionsMap.put("1",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",0);
        question.put("opt3",2);
        question.put("type",Constant.HealthEnum.DRINK);
        questionsMap.put("2",question);

        question = new HashMap<>();
        question.put("opt1",2);
        question.put("opt2",1);
        question.put("opt3",0);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("3",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("opt4",3);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("4",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",1);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("5",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("6",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",1);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("7",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",1);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("8",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("opt4",2);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("9",question);

        question = new HashMap<>();
        question.put("opt1",2);
        question.put("opt2",2);
        question.put("opt3",1);
        question.put("opt4",0);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("10",question);

        question = new HashMap<>();
        question.put("opt1",2);
        question.put("opt2",2);
        question.put("opt3",1);
        question.put("opt4",0);
        question.put("type",Constant.HealthEnum.FOOD);
        questionsMap.put("11",question);

        question = new HashMap<>();
        question.put("opt1",5);
        question.put("opt2",4);
        question.put("opt3",3);
        question.put("opt4",0);
        question.put("type",Constant.HealthEnum.SPORT);
        questionsMap.put("12",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("type",Constant.HealthEnum.SPORT);
        questionsMap.put("13",question);

        question = new HashMap<>();
        question.put("opt1",2);
        question.put("opt2",1);
        question.put("opt3",0);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("14",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("15",question);

        question = new HashMap<>();
        question.put("opt1",4);
        question.put("opt2",2);
        question.put("opt3",0);
        question.put("opt4",2);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("16",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("opt4",3);
        question.put("opt5",3);
        question.put("type",Constant.HealthEnum.PRESSURE);
        questionsMap.put("17",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",1);
        question.put("opt4",2);
        question.put("opt5",3);
        question.put("type",Constant.HealthEnum.PRESSURE);
        questionsMap.put("18",question);

        question = new HashMap<>();
        question.put("opt1",3);
        question.put("opt2",2);
        question.put("opt3",1);
        question.put("opt4",1);
        question.put("opt5",0);
        question.put("type",Constant.HealthEnum.PRESSURE);
        questionsMap.put("19",question);

        question = new HashMap<>();
        question.put("opt1",3);
        question.put("opt2",2);
        question.put("opt3",1);
        question.put("opt4",1);
        question.put("opt5",0);
        question.put("type",Constant.HealthEnum.PRESSURE);
        questionsMap.put("20",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("opt4",3);
        question.put("opt5",4);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("21",question);

        question = new HashMap<>();
        question.put("opt1",1);
        question.put("opt2",0);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("22",question);

        question = new HashMap<>();
        question.put("opt1",1);
        question.put("opt2",0);
        question.put("opt3",2);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("23",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("opt4",3);
        question.put("opt5",0);
        question.put("opt6",1);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("24",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("opt3",2);
        question.put("opt4",0);
        question.put("opt5",1);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("25",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("26",question);

        question = new HashMap<>();
        question.put("opt1",0);
        question.put("opt2",1);
        question.put("type",Constant.HealthEnum.ALL);
        questionsMap.put("27",question);

    }

}